#!/usr/bin/perl

require "accounts.pl";

&ReadParse();

local $username = "";
local $fullname = "";
local $password = "";
local $confpass = "";
local @joingroup = ();
local $disable = 0;
local $i=0;
local $page_title;

local $td_width=150;

if($in{'buttonsubmit'} eq 'modify')
{
	$page_title = $text{'modify_user_title'};
	&header($page_title, "", "modifyuser", 0, 1);
}
if($in{buttonsubmit} eq 'create')
{
	$page_title = $text{'create_user_title'};
	&header($page_title, "", "createuser", 0, 1);
}

print "<form name=form_edit_user action=index.cgi method=post onsubmit='return onsubmit_form(\"$in{buttonsubmit}\");'>\n";


if($in{'buttonsubmit'} eq 'modify')
{
	$username=$in{'username'};
	print "<input name=oldusername type=hidden value=\"$username\">\n";
}

print "<table class=mode1 width=80%>";
print "<tr><td width=$td_width>$text{'user_name'}</td><td><input name=username class=text type='text' value=\"$username\"></td></tr>";
if($in{'buttonsubmit'} eq 'modify')
{
	print "<tr><td width=$td_width>$text{'full_name'}</td><td><input name=fullname class=text type='text' value=\"$in{'fullname'}\"> ";
}
else
{
	print "<tr><td width=$td_width>$text{'full_name'}</td><td><input name=fullname class=text type='text' value=''> ";
}

if($in{buttonsubmit} eq 'create')
{
	#print "<tr><td width=$td_width>$text{'setpass_req'}</td><td><input name=setpass type='radio' value=$text{'setpass_ack_yes'}>$text{'setpass_ack_yes'}<input name=setpass type='radio' value=$text{'setpass_ack_no'}>$text{'setpass_ack_no'}</td></tr>";
	print "<tr><td width=$td_width>$text{'password'}</td><td><input class=text name=passwd type='password' value=''>\n";
	print "<tr><td width=$td_width>$text{'conf_pass'}</td><td><input class=text name=conf_pass type='password' value=''>\n";
}
if($in{buttonsubmit} eq 'modify')
{
	if($in{"disabled"} eq "smb_err")
	{
		print "<tr><td width=$td_width>$text{'password'}</td><td><input class=text name=passwd type='password' value=''>\n";
		print "<tr><td width=$td_width>$text{'conf_pass'}</td><td><input class=text name=conf_pass type='password' value=''>\n";		
	}
	else
	{
		print "<tr><td width=$td_width>$text{'setpass_req'}</td><td><input name=setpass type='radio' value=$text{'setpass_ack_yes'} onclick='on_click_radio_setpass_yes();'>$text{'setpass_ack_yes'}<input name=setpass type='radio' value=$text{'setpass_ack_no'} checked onclick='on_click_radio_setpass_no();'>$text{'setpass_ack_no'}</td></tr>";
		print "<tr><td width=$td_width>$text{'password'}</td><td><input class=disabletext name=passwd type='password' value='' disabled=true>\n";
		print "<tr><td width=$td_width>$text{'conf_pass'}</td><td><input class=disabletext name=conf_pass type='password' value='' disabled=true>\n";
	}
}

print "<tr><td width=$td_width valign=top>$text{'join_group'}</td>\n";

if($in{buttonsubmit} eq 'create')
{
	my @joingroup = ();
	my $i;
	open(LOCALGROUP, $config{"local_group_file"});
	@joingroup=<LOCALGROUP>;
	close(LOCALGROUP);

	print "<td>\n";
	print "<table border=0 class=mode1>";
	print "<tr><td>$text{'edit_user_lable_list_added'}</td><td></td><td>$text{'edit_user_lable_list_toadd'}</td></tr>";
	print "<tr><td>\n";
	print "<select class=list2 name=localgrouplist size=7 multiple>\n";
	print "</select>\n";
	print "<input name=listinput type=hidden value = ''>";
	print "</td><td>";
	print "<table><tr><td><input class=sbutton type=button onclick='addallselect();' value=\'$text{'edit_user_button_addall'}\'></td></tr>\n";
	print "<tr><td><input class=sbutton type=button onclick='removeallselect();' value=\'$text{'edit_user_button_removeall'}\'></td></tr>\n";
	print "<tr><td><input class=sbutton type=button onclick='addselect();' value=\'$text{'edit_user_button_add'}\'></td></tr>\n";
	print "<tr><td><input class=sbutton type=button onclick='removeselect();' value=\'$text{'edit_user_button_remove'}\'></td></tr>\n";
	print "</table>\n";
	print "</td><td>\n";
	print "<select class=list2 name=localgrouplist2 size=7 multiple>\n";
	$i = 0;
	while($i < @joingroup)
	{
		chomp($joingroup[$i]);
		print "<option value=\'$joingroup[$i]\'>$joingroup[$i]\n";
		$i++;
	}
	print "</select>\n";
	print "</td></tr></table>\n";
	#print "<input name=groupentry value=''>";
	#print &local_select_group_chooser_button("localgrouplist", 1, "groupentry");
	print "</td></tr>\n";
	#print "<input type='button' onclick='on_chooser_click();' value='Choose'></td></tr>";

}

if($in{buttonsubmit} eq 'modify')
{
	my @groups = &get_user_grouplist($username);
	my @joingroup = ();
	my $i;
	my $entrygroup = "";
	open(LOCALGROUP, $config{"local_group_file"});
	@joingroup=<LOCALGROUP>;
	close(LOCALGROUP);

	@joingroup = sort @joingroup;

	print "<td>\n";
	print "<table class=mode1>";
	print "<tr><td>$text{'edit_user_lable_list_added'}</td><td></td><td>$text{'edit_user_lable_list_toadd'}</td></tr>";
	print "<tr><td>\n";
	print "<select name=localgrouplist class=list2 size=7 multiple class=list>\n";
	$i = 0;
	while ($i < @groups)
	{
		print "<option value = $groups[$i]>$groups[$i]";
		$i++;
	}
	$entrygroup = join(" ", @groups);
	print "</select>\n";
	print "<input name=listinput type=hidden value = \'$entrygroup\'>";
	print "</td><td>";
	print "<table><tr><td><input class=sbutton type=button onclick='addallselect();' value=\'$text{'edit_user_button_addall'}\'></td></tr>\n";
	print "<tr><td><input class=sbutton type=button onclick='removeallselect();' value=\'$text{'edit_user_button_removeall'}\'></td></tr>\n";
	print "<tr><td><input class=sbutton type=button onclick='addselect();' value=\'$text{'edit_user_button_add'}\'></td></tr>\n";
	print "<tr><td><input class=sbutton type=button onclick='removeselect();' value=\'$text{'edit_user_button_remove'}\'></td></tr>\n";
	print "</table>\n";
	print "</td><td>\n";
	print "<select name=localgrouplist2 class=list2 size=7 multiple>\n";
	$i = 0;
	while($i < @joingroup)
	{
		chomp($joingroup[$i]);
		print "<option value=\'$joingroup[$i]\'>$joingroup[$i]\n";
		$i++;
	}
	print "</select>\n";
	print "</td></tr></table>\n";
	#print "<input name=groupentry value=''>";
	#print &local_select_group_chooser_button("localgrouplist", 1, "groupentry");
	print "</td></tr>\n";
	#print "<input type='button' onclick='on_chooser_click();' value='Choose'></td></tr>";
}

print "<tr><td></td>\n";
if($in{buttonsubmit} eq 'create')
{
	print "<td><input name=disabled type=checkbox value='no'>$text{'account_disable'}</td>\n";
	print "</tr>\n";
}

if($in{buttonsubmit} eq 'modify')
{
	if($in{"disabled"} eq "no")
	{
		print "<td><input name=disabled type=checkbox value='no'>$text{'account_disable'}</td>\n";
		print "</tr>\n";
	}
	elsif($in{"disabled"} eq "yes")
	{
		print "<td><input name=disabled type=checkbox value='yes' checked>$text{'account_disable'}</td>\n";
		print "</tr>\n";
	}
	elsif($in{"disabled"} eq "smb_err")
	{
		print "<td><input name=disabled type=checkbox value='no'>$text{'account_disable'}</td>\n";
		print "</tr>\n";
	}
}

print "</table>\n";

print "<input name=submitbutton type=hidden value=\"$in{'buttonsubmit'}\">";

print "<table border=0 class=mode1>\n";
print "<tr><td>\n";
if($in{buttonsubmit} eq 'create')
{
	print "<input type=submit name=Create class=button1 value=\"$text{'edit_user_submit_create'}\" onclick='return on_create_click(\"$text{'error_msg_no_username'}\", \"$text{'error_msg_passwd_noconf'}\");'>";
}
else
{
	print "<input type=submit name=Modify class=button1 value=\"$text{'edit_user_submit_modify'}\" onclick='return on_modify_click(\"$text{'error_msg_no_username'}\", \"$text{'error_msg_passwd_noconf'}\");'>";
}
print "</td><td>\n";
print "<input type = submit class=button1 name=cancle value=\'$text{'edit_user_submit_cancle'}\' onclick='return on_cancel_click();'>\n";
print "</td></tr>";
print "</table>\n";

print "</form>";

#if ($in{'buttonsubmit'} eq 'create')
#{
print <<EOF;
<script>
function onsubmit_form(btn)
{
        if(btn == "create")
        {
                document.forms[0].Create.disabled = true;
        }
        if(btn == "modify")
        {
                document.forms[0].Modify.disabled = true;
        }

        document.forms[0].cancle.disabled = true;
}

function on_groupentry_change()
{
//alert("aa");
}

function on_chooser_click()
{
	//var i;
	chooser = window.open("/local_group_chooser.cgi?multi=1&group=abc bbb", "chooser", "toolbar=no,menubar=no,scrollbars=yes,width=500,height=200");
	//for(i = 0; i < document.forms[0].localgrouplist.options.length; i++)
//	#{
//#		document.forms[0].localgrouplist.options[i] = null;
//#	}
//#	for(i = 0; i < chooser.sel.length; i++)
//#	{
//#		i = document.forms[0].localgrouplist.options.length;
//#		document.forms[0].localgrouplist.options[i] = new Option(chooser.sel[i], chooser.sel[i]);
//#	}
//document.forms[0].hostlist.options[i] = new Option(entrytext, hostentry);
//#	return true;
}

function find_item_in_grouplist(text1)
{
	var rt = 0;
	var ii;
	for(ii = 0; ii < document.forms[0].localgrouplist.options.length; ii++)
	{
		if(document.forms[0].localgrouplist.options[ii].text == text1)
		{
			rt = 1;
		}
	}
	return rt;
}

function addallselect()
{
	for(i = document.forms[0].localgrouplist.options.length; i > 0; i--)
	{
		document.forms[0].localgrouplist.options[i - 1] = null;
	}

	for(i = 0; i < document.forms[0].localgrouplist2.options.length; i++)
	{
		var text1 = document.forms[0].localgrouplist2.options[i].text;
		var option1 = new Option(text1,text1);
		document.forms[0].localgrouplist.options[i] = option1;
	}

	if(document.forms[0].localgrouplist.options.length > 0)
	{
		document.forms[0].listinput.value = document.forms[0].localgrouplist.options[0].text;
	}
	else
	{
		document.forms[0].listinput.value = "";
	}

	for(i = 1; i < document.forms[0].localgrouplist.options.length; i++)
	{
		document.forms[0].listinput.value += " " + document.forms[0].localgrouplist.options[i].text;
	}
}

function removeallselect()
{
	for(i = document.forms[0].localgrouplist.options.length; i > 0; i--)
	{
		document.forms[0].localgrouplist.options[i - 1] = null;
	}

	document.forms[0].listinput.value = "";
}

function addselect()
{
	var j= document.forms[0].localgrouplist.options.length;

	for(i = document.forms[0].localgrouplist2.options.length; i > 0; i--)
	{
		if(document.forms[0].localgrouplist2.options[i - 1].selected)
		{

			if(find_item_in_grouplist(document.forms[0].localgrouplist2.options[i - 1].text) == 0)
			{
				document.forms[0].localgrouplist.options[j] = new Option(document.forms[0].localgrouplist2.options[i - 1].text, document.forms[0].localgrouplist2.options[i - 1].text);
				j = document.forms[0].localgrouplist.options.length;
			}
		}
	}

	if(document.forms[0].localgrouplist.options.length > 0)
	{
		document.forms[0].listinput.value = document.forms[0].localgrouplist.options[0].text;
	}
	else
	{
		document.forms[0].listinput.value = "";
	}

	for(i = 1; i < document.forms[0].localgrouplist.options.length; i++)
	{
		document.forms[0].listinput.value += " " + document.forms[0].localgrouplist.options[i].text;
	}


}

function removeselect()
{
	for(i = document.forms[0].localgrouplist.options.length; i > 0; i--)
	{
		if(document.forms[0].localgrouplist.options[i - 1].selected)
		{
			document.forms[0].localgrouplist.options[i - 1] = null;
		}
	}

	if(document.forms[0].localgrouplist.options.length > 0)
	{
		document.forms[0].listinput.value = document.forms[0].localgrouplist.options[0].text;
	}
	else
	{
		document.forms[0].listinput.value = "";
	}

	for(i = 1; i < document.forms[0].localgrouplist.options.length; i++)
	{
		document.forms[0].listinput.value += " " + document.forms[0].localgrouplist.options[i].text;
	}
	//history.go(0);
}

function on_change_select()
{
}

function changeselect()
{
}

function on_create_click(error_msg_no_username, error_msg_passwd_noconf)
{
	//if(document.forms[0].username.text == "" || document.forms[0].password.text="")
	//{
	//	alert(error_msg_no_passwd);
	//	return false;
	//}
	if(document.forms[0].username.value =="")
	{
			alert(error_msg_no_username);
			return false;
	}

	if(document.forms[0].passwd.value != document.forms[0].conf_pass.value)
	{
		alert(error_msg_passwd_noconf);
		return false;
	}

	document.forms[0].action = "create_user.cgi";
	document.forms[0].submitbutton.value = "create";

}

function on_modify_click(error_msg_no_username, error_msg_passwd_noconf)
{
	if(document.forms[0].username.value =="")
	{
		alert(error_msg_no_username);
		return false;
	}

	if(document.forms[0].passwd.value != document.forms[0].conf_pass.value)
	{
		alert(error_msg_passwd_noconf);
		return false;
	}
	document.forms[0].action = "modify_user.cgi";
	document.forms[0].submitbutton.value = "modify";
}

function on_cancel_click()
{
	document.forms[0].action = "index.cgi";
	document.forms[0].submitbutton.value = "cancel";
}

function on_submit(error_msg_no_username, error_msg_passwd_noconf)
{
	if(document.forms[0].submitbutton.value == "modify")
	{
		return on_modify_click(error_msg_no_username, error_msg_passwd_noconf);
	}
	if(document.forms[0].submitbutton.value == "create")
	{
		return on_create_click(error_msg_no_username, error_msg_passwd_noconf);
	}
	return on_cancel_click();
}

function on_click_radio_setpass_yes()
{
	document.forms[0].passwd.disabled = false;
	document.forms[0].conf_pass.disabled = false;
	document.forms[0].passwd.style.backgroundColor = "#ffffff";
	document.forms[0].conf_pass.style.backgroundColor = "#ffffff";
}

function on_click_radio_setpass_no()
{
	document.forms[0].passwd.disabled = true;
	document.forms[0].conf_pass.disabled = true;
	document.forms[0].passwd.value = "";
	document.forms[0].conf_pass.value = "";
	document.forms[0].passwd.style.backgroundColor = "#d4d0c8";
	document.forms[0].conf_pass.style.backgroundColor = "#d4d0c8";
}

</script>
EOF
#}

&footer("/accounts/", $text{'index'});
